<?php

class BookingController extends Controller
{
    public function filters()
	{
		return array(
			'accessControl',
		);
	}
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array(''),
				'users'=>array('*'),
			),
			array('allow',
				'actions'=>array('index', 'check', 'unCheck'),
				'users'=>array('@'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
	}
	public function actionIndex($id)
	{
        $this->layout = '//layouts/eventsBookingIndex';
        $model = Events::model()->findByPk($id);
        
        $seatBlocks = SeatBlocks::model()->findAllByAttributes(array('place_id'=>1));
        $seatNotes = SeatNotes::model()->findAllByAttributes(array('place_id'=>1));
		$this->render('index', array(
            'model'=>$model,
            'seatBlocks'=>$seatBlocks,
            'seatNotes'=>$seatNotes
        ));
	}
    
    public function actionCheck()
    {
        if (isset($_POST['id'])) {
            $model = SeatTickets::model()->findByAttributes(array('seat_id'=>$_POST['id'], 'ticket_id'=>$_POST['ticket']));
            if (!isset($model)) {
                throw new CHttpException(404,'The requested page does not exist.');
            }
            
            if ($model->status === 'A') {
                echo json_encode(array(
                    'status'=>'1',
                    'id'=>$model->id,
                    'title'=>$model->seat->block->title . $model->seat->number,
                    'price'=>number_format($model->ticket->price, 0, '', ''),
                    'message'=>'',
                ));
            }
            else {
                echo json_encode(array(
                    'status'=>'0',
                    'message'=>'',
                ));
            }
        }
    }
    
    public function actionUnCheck()
    {
        if (isset($_POST['id'])) {
            $model = SeatTickets::model()->findByAttributes(array('seat_id'=>$_POST['id'], 'ticket_id'=>$_POST['ticket']));
            if (!isset($model)) {
                throw new CHttpException(404,'The requested page does not exist.');
            }

            echo json_encode(array(
                'status'=>'1',
                'id'=>$model->id,
                'title'=>$model->seat->block->title . $model->seat->number,
                'price'=>number_format($model->ticket->price, 0, '', ''),
                'message'=>'',
            ));
        }
    }
}